import 'package:casdon/common/utils/string_externtion.dart';
import 'package:casdon/modules/recipe/data/api/recipe_module_content_api.dart';
import 'package:casdon/modules/recipe/data/model/recipe_placeholder_data.dart';
import 'package:casdon/modules/recipe/ui/widgets/Recipe_card_item.dart';

// TODO： sort 添加排序字段
class RecipeModuleContentModel extends RecipeCardData {
  final String? _imgUrl; // 封面图
  final String? name; // 菜谱集或活动或烹饪课程名称
  @override
  final String? url; // 活动链接，
  final String id;
  @override
  final String recipeId;
  final int isControl;
  final String view;
  final String collect;
  final int sort;
  @override
  final String count;
  @override
  final bool isVideo;

  RecipeModuleContentModel({
    required String? imgUrl,
    required this.name,
    required this.url,
    required this.id,
    required this.recipeId,
    required this.isControl,
    required this.view,
    required this.collect,
    required this.count,
    required this.sort,
    required this.isVideo,
  }) : _imgUrl = imgUrl;

  // 静态方法转换
  static RecipeModuleContentModel fromApi(RecipeModuleContent response) {
    return RecipeModuleContentModel(
      imgUrl: _generateCoverImg(response),
      name: response.name ?? "",
      url: response.url,
      id: response.id,
      isVideo: (response.videoCoverUrl ?? "").isEmpty ? false : true,
      recipeId: response.recipeId ?? '',
      isControl: response.isControl ?? 0,
      view: (response.view ?? "").isEmpty ? "0" : response.view!,
      collect: (response.collect ?? "").isEmpty ? "0" : response.collect!,
      count: (response.count ?? "").isEmpty ? "0" : response.count!,
      sort: response.sort ?? 0,
    );
  }

  static String _generateCoverImg(RecipeModuleContent response) {
    final rawVideoCoverUrl = response.videoCoverUrl;
    if (rawVideoCoverUrl == null || rawVideoCoverUrl.trim().isEmpty) {
      final rawImgUrl = response.imageUrl;
      if (rawImgUrl == null || rawImgUrl.trim().isEmpty) {
        return recipePlaceHolderImgUrl;
      }
      return rawImgUrl;
    }
    return rawVideoCoverUrl;
  }

  // 批量转换的静态方法
  static List<RecipeModuleContentModel> listFromApi(
    List<RecipeModuleContent> items,
  ) {
    return items.map(fromApi).toList();
  }

  @override
  bool get isSmart => (isControl ?? 0) == 1 ? true : false;

  @override
  String get imgUrl => (_imgUrl ?? recipePlaceHolderImgUrl).cropParams700;

  @override
  String get viewCount => view;

  @override
  String get title => name ?? "";

  @override
  RecipeCardData placeholder() {
    // TODO: implement placeholder
    throw UnimplementedError();
  }
}
